package com.party.mobile.biz.course;

import com.party.core.model.distributor.DistributorDetail;
import com.party.core.model.distributor.DistributorDetailType;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.member.MemberAct;
import com.party.core.service.distributor.IDistributorDetailService;
import com.party.core.service.member.IMemberActService;
import com.party.mobile.biz.distributor.DistributorBizService;
import com.party.mobile.web.dto.crowdfund.input.StatisticsInput;
import com.party.mobile.web.dto.distributor.input.ApplyDistributorInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;

/**
 * @ClassName CourseBizService
 * @Description 课程biz
 * @Author yifeng
 * @Date 2019/4/19 9:25
 **/
@Service
public class CourseBizService {
    @Autowired
    private DistributorBizService distributorBizService;
    @Autowired
    private IMemberActService memberActService;
    @Autowired
    private IDistributorDetailService distributorDetailService;

    /**
     * @param input         参数
     * @param disRelation   分销关系
     * @param applyMemberId 报名者id
     * @param fn            回调函数
     * @return
     * @Author yifeng
     * @Description 维护分销关系，分销数据
     * @Date 10:05 2019/4/19
     **/
    public void statistics(StatisticsInput input, DistributorRelation disRelation, String applyMemberId, Function fn) {
        if (null != disRelation) {
            Integer findCount = 0;

            MemberAct query = new MemberAct();
            query.setMemberId(applyMemberId);
            query.setActId(input.getDistributorTargetId());
            List<MemberAct> memberActs = memberActService.list(query);
            for (MemberAct item : memberActs) {
                DistributorDetail applyDetail = distributorDetailService.get(item.getOrderId()
                        , DistributorDetailType.DISTRIBUTOR_TYPE_ORDER.getCode(), disRelation.getId());
                if (null != applyDetail) {
                    findCount++;
                    distributorDetailService.delete(applyDetail.getId());
                }
            }

            ApplyDistributorInput applyDistributorInput = new ApplyDistributorInput();
            applyDistributorInput.setDistributorTargetId(disRelation.getTargetId());
            applyDistributorInput.setDistributorType(disRelation.getType());
            applyDistributorInput.setDistributorId(disRelation.getDistributorId());
            applyDistributorInput.setDistributorParentId(disRelation.getParentId());
            applyDistributorInput.setTargetId(input.getOrderId());
            applyDistributorInput.setType(DistributorDetailType.DISTRIBUTOR_TYPE_ORDER.getCode());
            boolean result = distributorBizService.setDistributorData(applyDistributorInput, disRelation);
            if (result && findCount == 0) {
                distributorBizService.distributorCount(disRelation.getId(), applyDistributorInput.getType(), input.getPayment(), false);
                fn.apply(null);
            }
        }
    }
}
